package com.iqiyi.pps.epg.core.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.thrift.TBase;
import org.apache.thrift.TException;
import org.apache.thrift.TSerializer;
import org.apache.thrift.protocol.TSimpleJSONProtocol;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author fangying
 * @version 12-3-7 - 下午6:18
 */
public class Constants {
    /**
     * channellog  特殊操作人 系统
     */
    public static final String SYSTEM_USER_SIGN = "_system_";

    public static final String QUERY_ALIAS_FIELD_NAME = "QUERY_ALIAS";
    /**
     * 状态：无效
     */
    public static final int STATUS_INVALID = 0;
    public static final String STATUS_INVALID_CN = "无效";
    public static final String STATUS_INVALID_EN = "INVALID";
    /**
     * 状态：有效
     */
    public static final int STATUS_VALID = 1;
    public static final String STATUS_VALID_CN = "有效";
    //超级管理员ID
    public static final long SUPER_ADMIN_ID = 1L;
    //默认系统用户
    public static final long SYSTEM_USER_ID = 2L;
    //管理员角色
    public static final long ROLE_ADMIN = 1;
    //事件
    public static final String EVENT_TYPE_SEND_MESSAGE = "EVENT_TYPE_SEND_MESSAGE";
    public static final String TASK_TYPE_MAMS_MATERIAL_UPLOAD_TRANSCODE = "mams_material_upload_transcode";
    public static final String TASK_TYPE_MAMS_MATERIAL_UPLOAD_ = "mams_material_upload_transcode";
    public static final String WORKFLOW_RESTORE_BATCH_RESTORE = "batch_restore";
    public static final String WORKFLOW_RESTORE_BATCH_RESTART = "batch_restart";
    public static final String TRANSCODE_TYPE_VIDEO = "video";
    public static final String TRANSCODE_TYPE_IMAGE = "image";
    public static final String CODE_SUC = "A00000"; //操作成功
    public static final String CODE_FAIL = "A00002"; //操作失败
    public static final String CODE_ERROR = "A00001"; //异常
    public static final String CODE_ERROR_AUTH = "A00401";//授权失败
    public static final String CODE_ERROR_PARAM = "EPG00100";//参数错误
    public static final String CODE_ERROR_PARAM_NOT_NULL = "EPG00101";//参数不能为空
    public static final String CODE_ERROR_STREAMNAME_EXIST = "EPG00102"; //流名称已存在
    public static final String CODE_ERROR_LIVECHANNELSTREAM_NOT_EXIST = "EPG00103"; //编码流数据不存在
    public static final String CODE_ERROR_LIVESTREAM_NOT_EXIST = "EPG00104"; //流数据不存在
    public static final String CODE_ERROR_STREAMIP_NOT_EQUALS = "EPG00105"; //FMS源地址不匹配
    public static final String CODE_ERROR_HTTP_RESULT = "EPG00106"; //HTTP返回失败
    public static final String CODE_ERROR_QIPUID_NOT_EXIST = "EPG00107"; //奇普ID不存在
    public static final String CODE_ERROR_CHANNELBASE_NOT_EXIST = "EPG00108"; //频道基本信息不存在
    public static final String CODE_ERROR_STREAMTYPE_EXIST = "EPG00109";  //清晰度已存在;
    public static final String CODE_ERROR_STREAMNAME = "EPG00110";  //流名称未编码无法创建
    public static final String CODE_ERROR_PEERCOUNT_MIN = "EPG00111";  //镜像服务器数不能小于等于0
    public static final String CODE_ERROR_PEERCOUNT_MAX = "EPG00112";  //镜像服务器数不能大于157
    public static final String CODE_ERROR_STREAM_STATUS_STOP = "EPG00113"; //直播流未启动，无法停止
    public static final String CODE_ERROR_DELETE_LIVECHANNELSTREAM_LESS = "EPG00114";  // 无法删除最后一条正在编码中的流
    public static final String CODE_ERROR_STREAMNAME_SIGN = "EPG00115";     //流名称命名不符合规范
    public static final String CODE_ERROR_BCSNUM_MIN = "EPG00116";  //广播源数量不能小于3
    public static final String CODE_ERROR_BCSNUM_MAX = "EPG00117";  //广播源数量不能大于10
    public static final String CODE_ERROR_HAVING_CURRENT_PROGRAM = "EPG00118";  //当前有正在直播的节目，无法停止或删除流信息
    public static final String CODE_ERROR_DOLBYSTREAMNAME_EXIST = "EPG00119"; //杜比流名称已存在
    public static final String CODE_ERROR_STREAM_ENCODETYPE = "EPG00120"; // 流类型错误 (0: h264, 1: h265)

    public static final String CODE_WARNING_LIVECHANNELSTREAM_STATUS = "EPG00201"; //频道流未编码
    public static final String CODE_WARNING_STREAMNAME = "EPG00202";  //用户创建的流名称，不在编码中
    public static final String CODE_WARNING_STREAMTYPE_EXIST = "EPG00203";  //已经存在相同清晰度的流，新创建的流已设置为下线
    public static final String CODE_WARNING_STREAMTYPE_ENABLED_AUTOOFFLINE = "EPG00204";  //已将相同清晰度的上线的流自动下线
    public static final String CODE_INFO_STREAM_CREATE_REPEAT = "EPG00301"; //直播流修改RTMP地址失败，请删除重新创建直播流
    public static final String HTTP_ERROR_P2POFFLINE_REPEAT = "p2pchannel offline repeat";  //p2p重复下线
    public static final String CODE_ERROR_FETCH_VERSION_INCOMPLETE = "EPG000121"; //返回版本参数数目错误

    public static final String CODE_ERROR_SIGNALNAME_EXIST = "EPG00501"; //信号名称已存在
    public static final String CODE_ERROR_SIGNAL_RELATED = "EPG00502"; //信号被关联


    public static final String EXTEND_NEW_EPG_PROGRAM = "new_epg_program";
    public static final String EXTEND_NEW_EPG_PROGRAM_SNAPSHOT = "new_epg_program_snapshot";
    public static final String EXTEND_NEW_EPG_CHANNEL = "new_epg_channel";
    private static ObjectMapper mapper = new ObjectMapper();

    private static final Map<String, String> msgMap = new HashMap();

    static {
        msgMap.put(CODE_SUC, "操作成功");
        msgMap.put(CODE_FAIL, "操作失败");
        msgMap.put(CODE_ERROR_AUTH, "授权失败");
        msgMap.put(CODE_ERROR_PARAM, "参数错误");
        msgMap.put(CODE_ERROR_PARAM_NOT_NULL, "参数不能为空");
        msgMap.put(CODE_ERROR_STREAMNAME_EXIST, "流名称已存在");
        msgMap.put(CODE_ERROR_LIVECHANNELSTREAM_NOT_EXIST, "编码流数据不存在");
        msgMap.put(CODE_ERROR_LIVESTREAM_NOT_EXIST, "流数据不存在");
        msgMap.put(CODE_ERROR_STREAMIP_NOT_EQUALS, "FMS源地址不匹配");
        msgMap.put(CODE_ERROR_HTTP_RESULT, "HTTP返回失败");
        msgMap.put(CODE_ERROR_QIPUID_NOT_EXIST, "奇普ID不存在");
        msgMap.put(CODE_ERROR_CHANNELBASE_NOT_EXIST, "频道基本信息不存在");
        msgMap.put(CODE_WARNING_LIVECHANNELSTREAM_STATUS, "频道流未编码");
        msgMap.put(CODE_ERROR_STREAMTYPE_EXIST, "编码流清晰度已存在");
        msgMap.put(CODE_ERROR_STREAMNAME, "流名称未编码无法创建");
        msgMap.put(CODE_ERROR_PEERCOUNT_MIN, "镜像服务器数不能小于等于0");
        msgMap.put(CODE_ERROR_PEERCOUNT_MAX, "镜像服务器数不能大于157");
        msgMap.put(CODE_WARNING_STREAMNAME, "用户创建的流名称，不在编码中");
        msgMap.put(CODE_ERROR_STREAM_STATUS_STOP, "直播流未启动，无法停止");
        msgMap.put(CODE_ERROR_DELETE_LIVECHANNELSTREAM_LESS, "无法删除最后一条正在编码中的流");
        msgMap.put(CODE_ERROR_STREAMNAME_SIGN, "流名称命名不符合规范");
        msgMap.put(CODE_ERROR_HAVING_CURRENT_PROGRAM, "当前有正在直播的节目，无法停止流信息");
        msgMap.put(CODE_ERROR_DOLBYSTREAMNAME_EXIST, "杜比流名称已存在");
        msgMap.put(CODE_ERROR_STREAM_ENCODETYPE, "流类型错误");
        msgMap.put(CODE_ERROR_FETCH_VERSION_INCOMPLETE, "返回版本参数数目错误");
    }

    public static Map<Integer, String> getStatusMap() {
        Map map = new HashMap<Integer, String>();
        map.put(STATUS_VALID, STATUS_VALID_CN);
        map.put(STATUS_INVALID, STATUS_INVALID_CN);
        return map;
    }

    /**
     * 状态码map
     *
     * @return map
     */
    public static String getCode(String codeKey) {
        String returnValue = msgMap.get(codeKey);
        return returnValue == null ? codeKey : returnValue;
    }

    //boolean、byte、char、short、int、long、float 和 double

    /**
     * 获取基本类型class的Map
     *
     * @return
     */
    public static Map<String, Class> getBaseTypeClassMap() {
        Map<String, Class> classMap = new HashMap<String, Class>();
        classMap.put("boolean", boolean.class);
        classMap.put("byte", byte.class);
        classMap.put("char", char.class);
        classMap.put("short", short.class);
        classMap.put("int", int.class);
        classMap.put("long", long.class);
        classMap.put("float", float.class);
        classMap.put("double", double.class);
        classMap.put("String", String.class);
        return classMap;
    }

    public static Map<String, Class> getBaseTypeComplexClassMap() {
        Map<String, Class> classMap = new HashMap<String, Class>();
        classMap.put("boolean", Boolean.class);
        classMap.put("byte", Byte.class);
        classMap.put("char", Character.class);
        classMap.put("short", Short.class);
        classMap.put("int", Integer.class);
        classMap.put("long", Long.class);
        classMap.put("float", Float.class);
        classMap.put("double", Double.class);
        classMap.put("String", String.class);
        return classMap;
    }

    public static String getJsonResponse(String code, Object value, String msg) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", code);
        if (value != null) {
            map.put("data", value);
        }
        if (StringUtils.isNotBlank(msg)) {
            map.put("msg", msg);
        }
        String result = null;
        try {
            result = mapper.writeValueAsString(map);
        } catch (JsonGenerationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JsonMappingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
    }

    public static String thriftModelToJsonString(Object value) {
        String result = null;
        if (null != value) {
            Class ifaceimplClass = value.getClass();
            Class<?>[] interfaces = ifaceimplClass.getInterfaces();

            if (interfaces[0] != null && interfaces[0].getName().equals("org.apache.thrift.TBase")) {//thrift 需要特殊处理
                TSerializer serializer = new TSerializer(new TSimpleJSONProtocol.Factory());
                try {
                    String data = serializer.toString((TBase) value);
                    JSONObject jsonObject = JSON.parseObject(data, JSONObject.class);
                    result = JSON.toJSONString(jsonObject);
                } catch (TException e) {
                    e.printStackTrace();
                }
            } else {
                result = JSON.toJSONString(value);
            }
        }
        return result;
    }


    /**
     * ticket_log  工单日志常量类型
     */
    public enum TicketLogType {
        TICKET_ADD(100), TICKET_EDIT(101), TICKET_ASSIGN(102),
        CHECK_LIVE(201), CHECK_LIVE_UPDATE(202);
        private int index;

        private TicketLogType(int index) {
            this.index = index;
        }

        public int getIndex() {
            return index;
        }
    }

    /**
     * vod_log  直转点日志常量类型
     */
    public enum VodLogType {
        VOD_ADD(101), VOD_DEL(102), VOD_EDIT(103),
        VOD_RECORD_START(104), VOD_RECORD_STOP(105),
        VOD_SUBMIT_LEGO(106), VOD_DODOT(107), VOD_APPEND(108), VOD_RE_DODOT(109),
        VOD_AUTO_RECORD_START(110), VOD_AUTO_RECORD_STOP(111),
        VOD_TASK_ADD(112), VOD_TASK_EDIT(113), VOD_TASK_DEL(114);
        private int index;

        private VodLogType(int index) {
            this.index = index;
        }

        public int getIndex() {
            return index;
        }
    }

    /**
     * program_director_log  导播台日志常量类型
     */
    public enum DirectorLogType {
        DIRECTOR_ADD(1), DIRECTOR_EDIT(2), DIRECTOR_DEL(3), RESOURCE_ADD(4), RESOURCE_EDIT(5), RESOURCE_DEL(6),
        DIRECTOR_START(7), DIRECTOR_STOP(8), DIRECTOR_SWITCH_BACKUP(9), DIRECTOR_SWITCH_RESOURCE(10),
        NOTIFY_START_PROGRAM(101), NOTIFY_UPDATE_PROGRAM(102), NOTIFY_REMOVE_SOURCE(103),
        NOTIFY_ADD_SOURCE(104), NOTIFY_SWITCH_SOURCE(105), NOTIFY_STOP_PROGRAM(106), NOTIFY_SWITCH_BACKUP(107),
        NOTIFY_ADD_EFFECT(109), NOTIFY_REMOVE_EFFECT(110), NOTIFY_UPDATE_EFFECT(111), NOTIFY_START_PREVIEW_EFFECT(112),
        NOTIFY_STOP_PREVIEW_EFFECT(113), NOTIFY_START_PLAY_EFFECT(114), NOTIFY_STOP_PLAY_EFFECT(115),
        NOTIFY_EPISODE_CHANGE(108), NOTIFY_START_PREVIEW_URL(116), NOTIFY_STOP_PREVIEW_URL(117),
        NOTIFY_START_PLAY_COMMENTARY(118), NOTIFY_STOP_PLAY_COMMENTARY(119), NOTIFY_COMMENTARY_CHANGE_VOLUMN(120)
        ;
        private int index;

        private DirectorLogType(int index) {
            this.index = index;
        }

        public int getIndex() {
            return index;
        }
    }

    /**
     * channellog  日志常量类型
     */
    public enum LogType {
        CHANNEL_PRODUCT_IMG(106), CHANNEL_PRODUCT_QITAN(107), CHANNEL_APC_ADD(108), CHANNEL_BEIBOKU_DELETE(109),
        NEWTS_CREATE(251), NEWTS_START(252), NEWTS_STOP(253), NEWTS_DELETE(254), NEWTS_RESTART(255),
        DOLBY_CREATE(241), DOLBY_START(242), DOLBY_STOP(243), DOLBY_DELETE(244), DOLBY_RESTART(245),
        FLV_CREATE(210), FLV_START(211), FLV_STOP(212), FLV_DELETE(213), FLV_RESTART(215),
        TS_CREATE(220), TS_START(221), TS_STOP(222), TS_DELETE(223), TS_RESTART(225),
        ENCODE_START(234), ENCODE_STOP(235), ONKEY_SWITCH_STREAM(256),
        PROGRAM_ADD(300), PROGRAM_EDIT(301), PROGRAM_DEL(302), PROGRAM_SYNC(303), PROGRAM_GET_TVMAO(304), UPDATE_TVMAO_DATA(305),
        PROGRAM_MOVE(306), PROGRAM_REFER_INSERT(307), PROGRAM_BATCH_DEL(308), PROGRAM_INDEX_MOVE(309), PROGRAM_TOGGLE_LOCK(310),
        PROGRAM_PRODUCT_IMG(311), PROGRAM_WRITE_QIPU(312), PROGRAM_EDIT_BEFORE(313), PROGRAM_EDIT_REQ(314),
        PRODUCT_AUTO_PUSH(315), PROGRAM_AUTO_BROADCAST(316), WRITE_EPISODE_SUMMARY(317),
        STREAM_SNAPSHOT_WRITE(318), STREAM_SNAPSHOT_START(319), STREAM_SNAPSHOT_STOP(320),
        STREAM_SNAPSHOT_INTERRUPT(321), MEIZI_SNAPSHOT_PUSH(322), MEIZI_SNAPSHOT_WRITE(323), BOSS_PAID_MESSAGE(324),
        PROGRAM_ADVERT_MODIFY(325), PROGRAM_EDIT_BY_SPORT(326), PROGRAM_UNBIND_SPORT(327), PROGRAM_BIND_SPORT(328),
        PROGRAM_THIRDPART_API_FAIL(329),PROGRAM_ADD_FEED(330),PROGRAM_CLEAR_FEED(331),
        RESERVATION_DEL(430), RESERVATION_ADD(400), RESERVATION_EDIT(420),
        AUTO_ONLINE(501), AUTO_OFFLINE(502), AUTO_ALL_ONLINE(503), AUTO_DELETE_CHANNEL(504),
        AUTO_SET_TAG(505), PROGRAM_BROADCAST(506), PROGRAM_BROADCAST_OFFLINE(507),
        AUTO_SYSTEM_DELETE_STREAM(508),
        CAROUSEL_CREATE(600), CAROUSEL_DEL(601), CAROUSEL_GET(602),
        CAROUSEL_AUTOMATION_CREATE(603),
        CAROUSEL_AUTOMATION_DEL(604),
        CAROUSEL_AUTOMATION_INDEX_MOVE(605),
        CAROUSEL_AUTOMATION_STATUS(606),
        CAROUSEL_EPISODE_BATCH_ADD(607),
        CAROUSEL_EPISODE_BATCH_DEL(608),
        CAROUSEL_EPISODE_INDEX_MOVE(609), CAROUSEL_EPISODE_CREATE(610), CAROUSEL_EPISODE_FOLLOW_UPDATE(611),
        CAROUSEL_EPISODE_SORT(612),
        CHAT_ROOM_CREATE(701), CHAT_ROOM_DEL(702), CHAT_ROOM_OPEN(711),CHAT_PACKAGE_HISTORY(712),
        DAOJU_ADD(703), DAOJU_LINK(704), THIRD_NOTIFY_CHANNEL_ENCODE(705), THIRD_NOTIFY_BOSS(706), THIRD_NOTIFY_PPC_HOT(707), CHAT_ROOM_UPDATE(708),
        BOSS_CALLBCAK(709), PIC_CALLBACK(710), NOTIFY_BOSS(711),
        QIPU_LISTEN_UPDATE(800), SEND_NOTICE_START(801),DIRECTOR_REGISTER_OUTPUT_URL(802),
        PROGRAM_STARTTIME_DELAY(900), PROGRAM_OFFLINE(901);
        private int index;

        private LogType(int index) {
            this.index = index;
        }

        public int getIndex() {
            return index;
        }
    }
}
